<?php
class model_news extends base_model{
	private $result = array(
		'state' => 404,
		'message' => 'error',
	);
    public function mod_category_list(){
        $parentsql = "select * from `@@news_category` WHERE `parentid`=0 order by sort asc";
        $parent = $this->db->query($parentsql,3);
        foreach($parent as $k=>$pv){
            $subsql = "select * from `@@news_category` WHERE `parentid`={$pv['id']} order by sort asc";
            $parent[$k]['sub'] = $this->db->query($subsql,3);

        }
        return $parent;
    }
    public function mod_category_item($id)
    {
        $sql = "select * from `@@news_category` WHERE `id`={$id} LIMIT 1";
        return $this->query($sql,2);
    }
    public function mod_news_category(){
        $parentsql = "select * from `@@news_category` WHERE `parentid`=0 order by sort asc";
        $parent = $this->db->query($parentsql,3);
        $categorys = array();
        foreach($parent as $pv){
            $categorys[$pv['id']]=$pv['title'];
            $subsql = "select * from `@@news_category` WHERE `parentid`={$pv['id']} order by sort asc";
            $sub = $this->db->query($subsql,3);
            foreach($sub as $v)
            {
                $categorys[$v['id']]='　- '.$v['title'];
            }
        }
        return $categorys;
    }
    public function mod_news_item($id){
        $sql = "SELECT * FROM `@@news` WHERE `id`={$id} LIMIT 1";
        return $this->db->query($sql,2);
    }
    public function mod_news_delete($id){
        $sql = "DELETE FROM `@@news` WHERE `id`={$id}";
        return $this->db->query($sql,0);
    }
    public function mod_news_delete_list($ids){
        $sql = "DELETE FROM `@@news` WHERE `id` IN ({$ids})";
        return $this->db->query($sql,0);
    }
	public function mod_news_list($args){
        $where_txt = "";
        if(!empty($args['categoryid'])){
            $where_txt .= " AND `category`=".$args['categoryid'];
        }
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@news` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => 'id,title,url,icon,`explain`,category,actiontime,reading,state,sort,counticon',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY `actiontime` DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
	}
	/*
	 * 查看单条文章
	 */
	public function mod_view($id){
		$sql = "SELECT * FROM `@@news` WHERE `id`={$id}";
		$data = $this->db->query($sql,2);
		//最新公告列表
		$cid = intval($this->config['sysnotice']);
		$pclass_sql = "select * from `@@news` where state=1 and category={$cid} order by sort desc";
		$topsysnotice = $this->db->query($pclass_sql,3);
		$sql = "SELECT `id`,`title` FROM `@@news` WHERE state=1 and `category`={$cid} ORDER BY `id` DESC LIMIT 10";
		$sysnotice = $this->db->query($sql,3);
		//浏览数加1
		$sql = "UPDATE `@@news` SET `reading`=`reading`+1 WHERE `id`={$id}";
		$this->db->query($sql,0);
		
		return array(
			'state' => 200,
			'message' => 'success',
			'data' => $data,
			'sysnotice' => $sysnotice,
			'topsysnotice' => $topsysnotice,
		);
	}
	
}